/**
 * 
 */
package com.taobao.wolfwolf.util;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Tux
 * 
 *         创建时间: 2011-5-9 下午01:45:49
 */
public class JSONProperty {
	private LinkedHashMap<Object, Object> map = new LinkedHashMap<Object, Object>();

	public JSONProperty() {
	}
	
	public void put(Object key, Object value) {
		map.put(key, value);
	}

	public String toJSONString() {
		StringBuffer buffer = new StringBuffer();

		buffer.append("{");
		for (Map.Entry<Object, Object> m : map.entrySet()) {
			Object o = m.getValue();
			
			buffer.append("\"" + m.getKey() + "\": ");
			if (o instanceof JSONArray) {
				String temp = ((JSONArray) o).toJSONString();
				if(temp.charAt(0) != '[')
					temp = temp.substring(1, temp.length() - 1);
				buffer.append(temp);
			} else if (o instanceof JSONObject) {
				buffer.append("[" + ((JSONObject) o).toJSONString() + "]");
			} else if (o instanceof String) {
				buffer.append("\"" + o + "\"");
			} else if (o instanceof Integer || o instanceof Float
					|| o instanceof Double || o instanceof Long) {
				buffer.append(o);
			} else if (o == null) {
				buffer.append("\"\"");
			}
			buffer.append(",");
		}
		buffer.append("}");
		buffer.deleteCharAt(buffer.length() - 2);

		return buffer.toString();
	}
}
